<?php


namespace Module\Member\Api\Controller;


use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\InputPackage;
use ModStart\Core\Input\Response;
use Module\Member\Auth\MemberUser;
use Module\Member\Support\MemberLoginCheck;
use Module\Member\Util\MemberAddressUtil;
use Module\Member\Util\MemberBlogUtil;
use Module\Member\Util\MemberUtil;
use Module\MemberCert\Util\MemberCertUtil;

class MemberController extends Controller implements MemberLoginCheck
{
//    public static $memberLoginCheckIgnores = ['register', 'loginMobile', 'bindUserBlog','setPayPass','bindUserAlipay','updateUser',"getUserInfo"];
    public static $memberLoginCheckIgnores = ['register', 'loginMobile'];

    public function current()
    {
        $data = [];
        $data['_certType'] = null;
        if (modstart_module_enabled('MemberCert')) {
            $data['_certType'] = MemberCertUtil::getCertType(MemberUser::id());
        }
        return Response::generateSuccessData($data);
    }


    /**
     * @return array
     * @throws BizException
     * 注册
     */

    public function register()
    {
        $input = InputPackage::buildFromInput();
        $data = [];
        $data['username'] = $input->getTrimString('username');
        $data['nickname'] = $input->getTrimString('nickname');
        $data['gender'] = $input->getTrimString('gender');
        $data['id_card'] = $input->getTrimString('id_card');
        $data['born'] = $input->getTrimString('born');
        $data['phone'] = $input->getTrimString('phone');
        $data['password'] = $input->getTrimString('pass');
        $data['code'] = $input->getTrimString('code');
        BizException::throwsIfEmpty('账号为空', $data['username']);
        BizException::throwsIfEmpty('姓名为空', $data['nickname']);
        BizException::throwsIfEmpty('性别为空', $data['gender']);
        BizException::throwsIfEmpty('身份证号为空', $data['id_card']);
        BizException::throwsIfEmpty('手机为空', $data['phone']);
        BizException::throwsIfEmpty('验证码为空', $data['code']);
        BizException::throwsIfEmpty('密码为空', $data['password']);
        BizException::throwsIfEmpty('出生年月为空', $data['born']);
//        Cache::put($data['phone'].'code',$data['code'],5);
        $codes = Cache::get($data['phone'] . 'code');
        if(!$codes){
            return Response::jsonError("验证码失效，请重新发送!");
        }
        if($codes != $data['code']){
            return Response::jsonError("您输入的验证码不正确!");
        }
        $cal = MemberUtil::uniqueCheck('phone', $data['phone']);
        if ($cal['code'] == '-1') {
            return Response::jsonError("手机号格式不正确!");
        }
//        if ($data['code'] != $codes) {
//            return Response::generateError("你输入的验证码有误!");
//        }
        //验证账号是否存在
        $num = MemberBlogUtil::getUserExist($data['username'], $data['phone']);
        if ($num == 1) {
            return Response::generateError("该账号已存在,不能重复注册!");
        }
        if ($num == 2) {
            return Response::generateError("该手机号已存在,不能重复注册!");
        }
        //开始注册
        $res = MemberBlogUtil::register($data);
        if ($res) {
            return Response::generateSuccess("注册成功!");
        }
        return Response::generateError("注册失败");
    }

    /***
     * 登录
     */
    public function loginMobile()
    {
        $input = InputPackage::buildFromInput();
        $data = [];
        $data['username'] = $input->getTrimString('username');
        $data['password'] = $input->getTrimString('pass');
        BizException::throwsIfEmpty('账号为空', $data['username']);
        BizException::throwsIfEmpty('密码为空', $data['password']);
        $result = MemberBlogUtil::login($data);
        if (is_array($result)) {
            return Response::jsonSuccessData($result);
        }
        return Response::generateError($result);
    }

    /***
     * @return array
     * @throws BizException
     * 绑定账户
     */
    public function bindUserBlog()
    {
        $input = InputPackage::buildFromInput();
        $data = [];
        $data['user_id'] = $input->getTrimString('user_id');
        $data['blog_id'] = $input->getTrimString('blog_id');
//        $data['pass'] = $input->getTrimString('pass');
        BizException::throwsIfEmpty('用户ID为空', $data['user_id']);
        BizException::throwsIfEmpty('钱币类型ID为空', $data['blog_id']);
//        BizException::throwsIfEmpty('支付密码为空', $data['pass']);
        $result = MemberBlogUtil::bindUserBlog($data);
        if (is_array($result)) {
            return Response::jsonSuccessData("绑定成功!");
        }
        return Response::generateError($result);
    }

    /**
     * @return array
     * @throws BizException
     * 绑定支付宝
     */
    public function bindUserAlipay()
    {
        $input = InputPackage::buildFromInput();
        $data = [];
        $data['user_id'] = $input->getTrimString('user_id');
        $data['ali_name'] = $input->getTrimString('ali_name');
        $data['pass'] = $input->getTrimString('pass');
        $data['ali_account'] = $input->getTrimString('ali_account');
        BizException::throwsIfEmpty('用户ID为空', $data['user_id']);
        BizException::throwsIfEmpty('支付宝名称为空', $data['ali_name']);
        BizException::throwsIfEmpty('支付宝账号为空', $data['ali_account']);
        BizException::throwsIfEmpty('支付密码为空', $data['pass']);
        $result = MemberBlogUtil::bindUserAlipay($data);
        if ($result == "1") {
            return Response::jsonSuccessData("绑定成功");
        }
        return Response::generateError($result);
    }

    /**
     * @return array
     * @throws BizException
     * 设置账号支付密码
     */
    public function setPayPass()
    {
        $input = InputPackage::buildFromInput();
        $data = [];
        $data['user_id'] = $input->getTrimString('user_id');
        $data['pass'] = $input->getTrimString('pass');
        if(strlen($data['pass']) != 6){
            BizException::throwsIfEmpty('密码长度不正确', $data['user_id']);
        }
        BizException::throwsIfEmpty('用户ID为空', $data['user_id']);
        BizException::throwsIfEmpty('支付密码为空', $data['pass']);
        BizException::throwsIf('密码长度不正确', strlen($data['pass']) != 6, $data['pass']);
        $result = MemberBlogUtil::setPayPass($data);
        if ($result == '1') {
            return Response::jsonSuccessData("设置成功");
        }
        return Response::generateError("设置失败");
    }

    /**
     * @return array
     * @throws BizException
     * 修改用户信息
     */
    public function updateUser()
    {
        $input = InputPackage::buildFromInput();
        $data = $input->all();
        BizException::throwsIfEmpty('用户ID为空', $data['user_id']);
        $id = $data['user_id'];
        unset($data['user_id']);
        unset($data['phone']);
        unset($data['username']);
        if(empty($data)){
            return Response::generateError("设置内容不能为空");
        }
        $result = MemberBlogUtil::updateUser($id,$data);
        if ($result == '1') {
            return Response::jsonSuccessData("设置成功");
        }
        return Response::generateError("设置失败");
    }
    
       /**
     * @return array
     * @throws BizException
     * 获取用户信息
     */
    public function getUserInfo()
    {
        $input = InputPackage::buildFromInput();
        $data = $input->all();
        BizException::throwsIfEmpty('用户ID为空', $data['user_id']);
        $id = $data['user_id'];
        $result = MemberBlogUtil::getUserDetail($id);
        if ($result) {
            return Response::jsonSuccessData($result);
        }
        return Response::generateError("获取失败");
    }

}
